package echat.future.cn.echat.my.adapter;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;

public class MyJumpDecoration extends RecyclerView.ItemDecoration{
    Context context;
    private int height;
    private Paint paint;

    public MyJumpDecoration(Context context,int height,int color){
        this.context = context;
        this.height =  (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,height,getContext().getResources().getDisplayMetrics());
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(color);
    }

    public Context getContext() {
        return context;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        if(parent.getChildCount() == parent.getChildAdapterPosition(view)){
            return;
        }
        outRect.bottom = height;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        //左右和父布局一样。所以这里我们获取到了左右位置了
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        int childcout = parent.getChildCount();
        for (int i = 0; i < childcout; i++) {
            //获取子控件，用来获取item距离父控件顶部高度
            View childerView = parent.getChildAt(i);
            //获取item的属性
            RecyclerView.LayoutParams parames = (RecyclerView.LayoutParams) childerView.getLayoutParams();
            //分割线上部位置
            int top = childerView.getBottom() + parames.bottomMargin + Math.round(ViewCompat.getTranslationY(childerView));
            int bottom = top + height;
            c.drawRect(new Rect(left,top,right,bottom),paint);
        }
    }
}
